package com.znsb.msfq.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.znsb.msfq.BaseActivity;
import com.znsb.msfq.R;
import com.znsb.msfq.app.ZnsbApp;
import com.znsb.msfq.bean.UserInfoBean;
import com.znsb.msfq.utils.CacheActivityUtils;
import com.znsb.msfq.utils.ConfigUtils;
import com.znsb.msfq.utils.DeviceUtils;
import com.znsb.msfq.utils.DialogUtils;
import com.znsb.msfq.utils.HttpUtils;
import com.znsb.msfq.utils.IntentUtils;
import com.znsb.msfq.utils.JsonResult;
import com.znsb.msfq.utils.LogUtils;
import com.znsb.msfq.utils.NetworkAddress;
import com.znsb.msfq.utils.OverallData;
import com.znsb.msfq.utils.ResponseUtils;
import com.znsb.msfq.utils.SPUtils;
import com.znsb.msfq.utils.ToastUtils;
import com.znsb.msfq.utils.UrlUtils;
import com.znsb.msfq.view.MySwitchView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @name：
 * @author： 孙科技
 * @phone： 13308334117
 * @createTime： 2017/2/9
 * @modifyTime： 2017/2/9
 * @explain：
 */

public class SetActivity extends BaseActivity {
    @Bind(R.id.bar_img_back)
    ImageView barImgBack;
    @Bind(R.id.bar_tv_title)
    TextView barTvTitle;
    @Bind(R.id.bar_tv_other)
    TextView barTvOther;
    @Bind(R.id.set_tv_version)
    TextView setTvVersion;
    @Bind(R.id.set_re_layout1)
    RelativeLayout setReLayout1;
    @Bind(R.id.set_re_layout2)
    RelativeLayout setReLayout2;
    @Bind(R.id.switch_view)
    MySwitchView switchView;
    @Bind(R.id.set_tv_loginout)
    TextView setTvLoginout;
    int ispush = 1;

    @Override
    protected int getlayoutView() {
        return R.layout.act_set;
    }

    @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    protected void initView(View view) {
        barTvTitle.setText("设置");
        ispush = SPUtils.getIsPush();
        if (ispush == 0) {
            switchView.setOn(true);
        } else {
            switchView.setOn(false);
        }
        switchView.setOnSwitchStateChangeListener(new MySwitchView.OnSwitchStateChangeListener() {
            @Override
            public void onSwitchStateChange(boolean isOn) {
                if (isOn) {
                    isPush(0);
                } else {
                    isPush(1);
                }

            }
        });
    }

    @Override
    protected void initData() {
        setTvVersion.setText(DeviceUtils.getversionName(this));

    }

    //是否收藏
    public void isPush(final int isPush) {

        Map<String, Object> maps = new HashMap<>();
        try {
            maps.put("isPush", isPush);//是否推送消息，0同意推送，1不同意推送
            HttpUtils.getInstance(this).postAsnyRequest(NetworkAddress.isPush, HttpUtils.getLoginMaps(maps), new ResponseUtils() {
                @Override
                public void onSucceed(String code, JSONObject request) {
                    LogUtils.i("", ">>>>>>isPush" + request);
                    SPUtils.saveToApp(ConfigUtils.INFO_ISPUSH, isPush);
                    if (isPush == 0) {
                        ToastUtils.TextToast(SetActivity.this, getResources().getString(R.string.push), 3000);
                    } else {
                        ToastUtils.TextToast(SetActivity.this, getResources().getString(R.string.nopush), 3000);
                    }
                }

                @Override
                public void onFailure(String result) {
                    LogUtils.i("", ">>>>>>onFailure" + result);
                }
            });
        } catch (Exception e) {

        }
    }

    @Override
    protected void clickListener(View view) {

    }

    @OnClick({R.id.bar_img_back, R.id.set_re_layout1, R.id.set_re_layout2, R.id.set_tv_loginout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bar_img_back:
                finish();
                break;
            case R.id.set_re_layout1:
                IntentUtils.startActivityAnimGeneral(this, SetPwdActivity.class, null);
                break;
            case R.id.set_re_layout2:
                IntentUtils.startActivityAnimGeneral(this, AboutUsActivity.class, null);
                break;
            case R.id.set_tv_loginout:
                DialogUtils.showPromptDialog(this, "系统提示", "是否确认退出登录？", "确认", null, "取消", new DialogUtils.OnMenuClick() {
                    @Override
                    public void onLeftMenuClick() {
                        loginOut();
                        SPUtils.clear(ZnsbApp.mContext);
                        CacheActivityUtils.finishOtherActivity(MainActivity.class);
                        IntentUtils.startActivityAnimGeneral(SetActivity.this, LoginActivity.class, null);
                    }

                    @Override
                    public void onCenterMenuClick() {

                    }

                    @Override
                    public void onRightMenuClick() {

                    }
                });
                break;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    private void loginOut() {
        HttpUtils.getInstance(this).postAsnyRequest(UrlUtils.MSFQ_URL_LOGINOUT, HttpUtils.getLoginMaps(null), new ResponseUtils() {
            @Override
            public void onSucceed(String Code, JSONObject request) {
                LogUtils.i("TAG:", "loginOut=" + request);
            }

            @Override
            public void onFailure(String result) {

            }
        });
    }

}
